package net.zjitc.android2019.ch2activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import net.zjitc.android2019.R;

public class Ch2CalcActivity extends AppCompatActivity {
    EditText ed;
    TextView tv;
    Button btn_back, btn_clear, btn_7, btn_8, btn_9, btn_div;
    Button btn_4, btn_5, btn_6, btn_mul, btn_1, btn_2, btn_3, btn_sub;
    Button btn_0, btn_point, btn_equal, btn_add;
    double n1,n2;
    String op;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch2_calc);
        init();
        View.OnClickListener click=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.ch2_calc_btn_0:
                    case R.id.ch2_calc_btn_1:
                    case R.id.ch2_calc_btn_2:
                    case R.id.ch2_calc_btn_3:
                    case R.id.ch2_calc_btn_4:
                    case R.id.ch2_calc_btn_5:
                    case R.id.ch2_calc_btn_6:
                    case R.id.ch2_calc_btn_7:
                    case R.id.ch2_calc_btn_8:
                    case R.id.ch2_calc_btn_9:
                        number(v);
                        break;
                    case R.id.ch2_calc_btn_point:
                        point();
                        break;
                    case R.id.ch2_calc_btn_add:
                    case R.id.ch2_calc_btn_sub:
                    case R.id.ch2_calc_btn_mul:
                    case R.id.ch2_calc_btn_div:
                        operator(v);
                        break;
                    case R.id.ch2_calc_btn_equal:
                        equal();
                        break;
                    case R.id.ch2_calc_btn_back:
                        back();
                        break;
                    case R.id.ch2_calc_btn_clear:
                        clear();
                        break;
                }
            }
            private void back() {
                String s=ed.getText().toString();
                s=s.substring(0,s.length()-1);
                if (!s.equals("")){
                    ed.setText(s);
                    ed.setSelection(s.length());
                }else {
                    tv.setText("再删就没啦！");
                }

            }
            private void clear() {
                tv.setText("");
                ed.setText("");
            }

            private void equal() {
                String  s2=ed.getText().toString();
                n2=Double.parseDouble(s2);
                double result=0;
                if (op.equals("+")){
                    result=n1+n2;
                }else if(op.equals("-")){
                    result=n1-n2;
                }else if(op.equals("8")){
                    result=n1*n2;
                }else if(op.equals("/")){
                    result=n1/n2;
                }
                String  s=tv.getText().toString();
                tv.setText(s+n2+"="+result);
                ed.setText(result+"");
            }

            private void operator(View v) {
                String  s1=ed.getText().toString();
                n1=Double.parseDouble(s1);
                Button button= (Button) v;
                op=button.getText().toString();
                tv.setText(n1+op);
                ed.setText("");
            }

            private void point() {
                String s=ed.getText().toString();
                if (!s.contains(".")){
                    s+=".";
                    ed.setText(s);
                    ed.setSelection(s.length());
                }

            }

            private void number(View v) {
                Button button= (Button) v;
                String s=ed.getText().toString()+button.getText().toString();
                ed.setText(s);
                ed.setSelection(s.length());
            }
        };
        btn_0.setOnClickListener(click);
        btn_1.setOnClickListener(click);
        btn_2.setOnClickListener(click);
        btn_3.setOnClickListener(click);
        btn_4.setOnClickListener(click);
        btn_5.setOnClickListener(click);
        btn_6.setOnClickListener(click);
        btn_7.setOnClickListener(click);
        btn_8.setOnClickListener(click);
        btn_9.setOnClickListener(click);
        btn_back.setOnClickListener(click);
        btn_clear.setOnClickListener(click);
        btn_add.setOnClickListener(click);
        btn_sub.setOnClickListener(click);
        btn_mul.setOnClickListener(click);
        btn_div.setOnClickListener(click);
        btn_point.setOnClickListener(click);
        btn_equal.setOnClickListener(click);
    }


        private void init() {
            ed = findViewById(R.id.ch2_calc_ed);
            tv = findViewById(R.id.ch2_calc_tv2);
            btn_0 = findViewById(R.id.ch2_calc_btn_0);
            btn_1 = findViewById(R.id.ch2_calc_btn_1);
            btn_2 = findViewById(R.id.ch2_calc_btn_2);
            btn_3 = findViewById(R.id.ch2_calc_btn_3);
            btn_4 = findViewById(R.id.ch2_calc_btn_4);
            btn_5 = findViewById(R.id.ch2_calc_btn_5);
            btn_6 = findViewById(R.id.ch2_calc_btn_6);
            btn_7 = findViewById(R.id.ch2_calc_btn_7);
            btn_8 = findViewById(R.id.ch2_calc_btn_8);
            btn_9 = findViewById(R.id.ch2_calc_btn_9);
            btn_back = findViewById(R.id.ch2_calc_btn_back);
            btn_clear = findViewById(R.id.ch2_calc_btn_clear);
            btn_add = findViewById(R.id.ch2_calc_btn_add);
            btn_sub = findViewById(R.id.ch2_calc_btn_sub);
            btn_mul = findViewById(R.id.ch2_calc_btn_mul);
            btn_div = findViewById(R.id.ch2_calc_btn_div);
            btn_point = findViewById(R.id.ch2_calc_btn_point);
            btn_equal = findViewById(R.id.ch2_calc_btn_equal);
    }

}
